<?php get_instance()->view('common/header');?>
<div class="page-container home">
    <?php get_instance()->view('common/sidebar');?>
    <div class="page-content-wrapper">
        <div class="page-content">
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                    <h3 class="page-title"> <?php echo $title ?> </h3>
                    <ul class="page-breadcrumb breadcrumb">
                        <li class="btn-group"> <a class="btn default ml_5 return" href="<?php echo getUrl('product/category/details?pid='.$category['parent_id'])?>"><i class="fa fa-chevron-left"></i> 返回</a> </li>
                        <li><i class="fa fa-home"></i><a href="<?php echo getUrl('product/category')?>">一级分类 <i class="fa fa-angle-right"></i></a></li>
                        <li><a href="<?php echo getUrl('product/category/details?pid='.$category['parent_id'])?>">二级分类 <i class="fa fa-angle-right"></i> </a></li>
                        <li><a href="javascript:void(0);">自定义分类 </a></li>
                    </ul>
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="portlet">
                        <div class="portlet-body">
                            <div class="table-container">
                                <div class="table-actions-wrapper"> <span> </span>
                                        <a class="btn btn-sm blue add" href="javascript:"><i class="fa fa-plus"></i> 新增</a>
                                </div>
                                <table class="table table-bordered table-hover" id="datatable">
                                    <thead>
                                    <tr role="row" class="heading">
                                        <th> 分类名称 </th>
                                        <th width="100px" sort="false">上级分类</th>
                                        <th width="100px" sort="false">所属商家</th>
                                        <th width="150px" sort="false">创建时间</th>
                                        <th width="100px" sort="false">状态</th>
                                        <th width="150px" sort="false">操作</th>
                                    </tr>
                                    <tr>
                                        <th><input class="form-control form-filter input-sm" name="cat_name" type="text"></th>
                                        <th></th>
                                        <th></th>
                                        <th>
                                            <div class="margin-bottom-5">
                                                <input class="form-control form-filter input-sm" name="send_date_from" dp placeholder="从" type="text">
                                            </div>
                                            <input class="form-control form-filter input-sm" name="send_date_to" dp placeholder="到" type="text" >
                                        </th>
                                        <th>
                                            <select name="status" class="form-control form-filter input-sm">
                                                <option value="" selected="selected">所有</option>
                                                <option value="2">启用</option>
                                                <option value="1">禁用</option>
                                            </select>
                                        </th>
                                        <th>
                                            <div class="btn-group">
                                                <a class="btn btn-xs filter-submit btn-default"><i class="fa fa-search"></i> 搜索</a>
                                                <button data-toggle="dropdown" class="btn btn-default dropdown-toggle btn-xs" type="button"><i class="fa fa-angle-down"></i></button>
                                                <ul role="menu" class="dropdown-menu pull-right">
                                                    <li><a href="javascript:void(0);" class="filter-cancel">重置</a></li>
                                                </ul>
                                            </div>
                                        </th>
                                    </tr>
                                    </thead>
                                    <tbody></tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="save-modal" class="modal fade modal_small" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
        <h4 class="modal-title"></h4>
      </div>
      <form action="<?php echo getUrl('product/category/saveCustom'); ?>" class="form-horizontal" id="save-category-form" method="post">
        <div class="modal-body">
			<!-- 商家分类名称 -->
			<div class="form-group">
				<label class="control-label col-md-2">分类名称<span class="required">*</span></label>
				<div class="col-md-9">
					<input name="cat_name" class="form-control required" />
				</div>
			</div>
			<!-- 搜索商家 -->
			<div class="form-group member_id">
				<label class="control-label col-md-2">所属商家<span class="required">*</span></label>
				<div class="col-md-9">
					<input class="form-control" name="merchant_id">
				</div>
			</div>
			
			<div class="form-group">
				<label class="control-label col-md-2">状态<span class="required">*</span></label>
				<div class="col-md-6" style="margin-left: 18px;">
					<div class="radio-list">
						<label class="radio-inline"><input name="status" checked value="2" type="radio"> 启用 </label> <label
							class="radio-inline"><input name="status" value="1" type="radio"> 禁用</label>
					</div>
				</div>
			</div>
			<!--  -->

		</div>
        <div class="modal-footer">
          <input type="hidden" name="id" value=""/>
          <input type="hidden" name="type" value="<?php echo $category['type']?>"/>
          <input type="hidden" name="category_id" value="<?php echo $category['id']?>"/>
          <button class="btn default" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i> 关闭</button>
          <button type="submit" class="btn green field-submit-btn"><i class="fa fa-save"></i> 保存</button>
          <script type="text/javascript">
          	$(function(){
              	$('select[name="county_id"]').select2();
                var defaultImg = '<?php echo getDefaultImg()?>';
				var model = $('#save-modal');

				model.find('[name="merchant_id"]').select2({
		            closeOnSelect: true,
		            placeholder: '商家昵称、电话',
		            minimumInputLength: 2,
		            ajax: {
		                url: "<?php echo getUrl('common/searchUserName')?>",
		                dataType: 'json',
		                data: function (term, page) {
		                    console.log
		                    return {
		                    	keyword: term, 
		                    };
		                },
		                results: function (data, page) {
			                console.log(data);
		                    return {
		                        results: data
		                    };
		                }
		            },
		            formatResult: function(movie) {
		            	return movie.name;
		            },
		            formatSelection: function(movie) {
		                return movie.name;
		            }, 
		            dropdownCssClass: "bigdrop",
		            escapeMarkup: function (m) {
		                return m;
		            }
		        });
		        
		        //头像插件
		        cropitMaster({
		            uploadBtn:$('.upload-btn'),
		            height:'92px',
		            width:'92px',
		            complete:function(btn,data){
		                $('#icn-img').attr('src', data);
		                $('#pic').val(data).change();
		            }
		        });
		        
				 
				$('.add').live('click', function(){
					model.find('.modal-title').html('新增分类');
					model.find('.has-error').removeClass('has-error').find('.validate_message').remove();
					model.find('.show_message_error').remove();

					model.find('#icn-img').attr('src',defaultImg);
					model.find('input[name="id"]').val('');
					model.find('input[name="cat_name"]').val('');
					model.find('input[name="sort"]').val(0);
					
					model.find('[name="merchant_id"]').val('');//设置选中
					model.find('.select2-chosen').text('商家昵称、电话').parent('a').addClass('select2-default');

					model.modal();
				});	
				
				$('.edit-btn').live('click', function(){
					model.find('.modal-title').html('编辑分类');
					model.find('.has-error').removeClass('has-error').find('.validate_message').remove();
					model.find('.show_message_error').remove();
					var url = $(this).attr('href');
					
					l_blockUI();
					$.get(url,{},function(data){
						data = eval('('+data+')');
						console.log(data)
						
						model.find('#icn-img').attr('src',data.pic);
						model.find('input[name="id"]').val(data.id);
						model.find('input[name="cat_name"]').val(data.cat_name);
						model.find('input[name="sort"]').val(data.sort);
						
						model.find('[name="merchant_id"]').val(data.merchant_id);//设置商家
						model.find('.select2-chosen').text(data.merchant_name).parent('a').removeClass('select2-default');

						//启用
						var status = (data.status == "2" ? 0 : 1);
						var status_a = status == 1 ? 0 : 1;
						model.find('input[name="status"]').eq(status).prop("checked",true).parent('span').addClass("checked");
						model.find('input[name="status"]').eq(status_a).prop("checked",false).parent('span').removeClass("checked");
						
						App.unblockUI();
						model.modal();
					})
					
					return false;
				});	
				
				
				$.e_validate.init('#save-category-form',{modal:model,ignore:''});
            });
          </script>
        </div>
      </form>
    </div>
  </div>
</div>
<script type="text/javascript">
    $(function(){
        datatable_init({
            src: $("#datatable"),
            sAjaxSource: "<?php echo getUrl('product/category/getCustomCategoryList?pid='.get('pid'));?>",
            aoColumnDefs : [{
                'bSortable' : false,
                'aTargets' : [0,1,2,4,5]
            }]
        });

        $('[dp]').not("[noinit]").datepicker({
            changeMonth: true,
            autoclose: true,
            format: "yyyy-mm-dd",
            language: 'zh-CN'
        });
    });
</script>
<script type="text/javascript">
    $(function () {
        $(".group").select2();
    });
</script>
</body>
</html>